﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jemgine.MapEditor.Controls
{
    public partial class BuildDialog : UserControl
    {
        public BuildDialog()
        {
            InitializeComponent();
            this.Enabled = false;
        }

        EditorState ES;
        WinFormsContentLoading.ContentBuilder Builder;

        internal void SetEditorState(EditorState ES)
        {
            this.ES = ES;
            ES.OnFileSet += OnFileSet;

            Builder = new WinFormsContentLoading.ContentBuilder();

            String ExeDir =
                System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(this.GetType()).Location);
            Builder.AddReference(System.IO.Path.Combine(ExeDir, "JemgineEngine.dll"));
        }

        void OnFileSet()
        {
            if (ES.FileSet)
            {
                this.Enabled = true;
            }
            else
            {
                this.Enabled = false;
            }
        }

        private void Build_Click(object sender, EventArgs e)
        {
            if (!ES.FileSet) return;
            ES.SaveMap();

            String AssetName = System.IO.Path.GetFileNameWithoutExtension(ES.FileName);

            Builder.Clear();

            Builder.Add(System.IO.Path.Combine(ES.Directory, ES.FileName), AssetName, "XmlImporter", "PassThroughProcessor");
            String Error = Builder.Build();

            if (Error != null) System.Windows.Forms.MessageBox.Show(Error);
            else
            {
                String BuiltPath = System.IO.Path.Combine(Builder.OutputDirectory, AssetName + ".xnb");
                String NewPath = System.IO.Path.Combine(ES._config.EngineFolder, ES._config.Episode);
                NewPath = System.IO.Path.Combine(NewPath, AssetName + ".xnb");
                System.IO.File.Copy(BuiltPath, NewPath, true);
            }

            System.Windows.Forms.MessageBox.Show("Map built.");
        }

        private void testButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(ES._config.Executable))
            {
                MessageBox.Show("Please set the name of the executable first.");
                return;
            }

            Build_Click(null, null);

            String CommandLine = "-Episode \"" + ES._config.Episode + "\" -StartMap \"" +
                System.IO.Path.GetFileNameWithoutExtension(ES.FileName) + "\"";
            if (!String.IsNullOrEmpty(testMap_NodeToFire.Text))
                CommandLine += " " + testMap_NodeToFire.Text;


            System.Diagnostics.Process Process = new System.Diagnostics.Process();
            Process.StartInfo.FileName = System.IO.Path.Combine(ES._config.EngineFolder, ES._config.Executable);
            Process.StartInfo.Arguments = CommandLine;
            Process.Start();
            Process.WaitForExit();

        }
    }
}
